﻿namespace QLPhongKham.BaseClass.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoDDMCachDung
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private decimal _desoluong;
        private long _iid;
        private int _iiddangnhap;
        private string _sma;
        private string _sSchema;
        private string _sten;

        public dtoDDMCachDung()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoDDMCachDung(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDDMCachDung.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where 1=1  and ", columnID, " = ", this._iid }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDDMCachDung.cs");
                return false;
            }
        }

        public long GetIDInsert()
        {
            long iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoDDMCachDung.cs");
                iDInsert = 1L;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public long GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                long num = 0L;
                try
                {
                    num = long.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return (num += 1L);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoDDMCachDung.cs");
                return 1L;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDDMCachDung.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnTEN, ",", columnMA, ",", columnSOLUONG, ",", columnIDDANGNHAP, ") values(", this._iid, 
                    ",:", columnTEN, ",'", this._sma, "',:", columnSOLUONG, ",", this._iiddangnhap, ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTEN, NpgsqlDbType.Text, 200).Value = this._sten;
                command.Parameters.Add(columnSOLUONG, NpgsqlDbType.Numeric, 7).Value = this._desoluong;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDDMCachDung.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._iid = 0L;
            this._sten = "";
            this._sma = "";
            this._desoluong = 0M;
            this._iiddangnhap = 0;
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoDDMCachDung.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnID, ",", columnTEN, ",", columnMA, ",", columnSOLUONG, ",", columnIDDANGNHAP, " from ", this._sSchema, ".", tableName, " where ", columnID, 
                    " = ", this._iid
                 }), connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._iid = long.Parse(dataTable.Rows[0][columnID].ToString());
                    this._sten = dataTable.Rows[0][columnTEN].ToString();
                    this._sma = dataTable.Rows[0][columnMA].ToString();
                    this._desoluong = decimal.Parse(dataTable.Rows[0][columnSOLUONG].ToString());
                    this._iiddangnhap = int.Parse(dataTable.Rows[0][columnIDDANGNHAP].ToString());
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoDDMCachDung.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoDDMCachDung.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnTEN + "," + columnMA + "," + columnSOLUONG + "," + columnIDDANGNHAP + " from " + this._sSchema + "." + tableName + " order by " + columnID, connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoDDMCachDung.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDDMCachDung.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnTEN, " = :", columnTEN, ",", columnMA, " = '", this._sma, "',", columnSOLUONG, "=:", columnSOLUONG, 
                    ",", columnIDDANGNHAP, "=", this._iiddangnhap, " where ", columnID, " = ", this._iid
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTEN, NpgsqlDbType.Text, 200).Value = this._sten;
                command.Parameters.Add(columnSOLUONG, NpgsqlDbType.Numeric, 7).Value = this._desoluong;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDDMCachDung.cs");
                return false;
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnIDDANGNHAP
        {
            get
            {
                return "iddangnhap";
            }
        }

        public static string columnMA
        {
            get
            {
                return "ma";
            }
        }

        public static string columnSOLUONG
        {
            get
            {
                return "soluong";
            }
        }

        public static string columnTEN
        {
            get
            {
                return "ten";
            }
        }

        public long pID
        {
            get
            {
                return this._iid;
            }
            set
            {
                this._iid = value;
            }
        }

        public int pIDDANGNHAP
        {
            get
            {
                return this._iiddangnhap;
            }
            set
            {
                this._iiddangnhap = value;
            }
        }

        public string pMA
        {
            get
            {
                return this._sma;
            }
            set
            {
                this._sma = value;
            }
        }

        public decimal pSOLUONG
        {
            get
            {
                return this._desoluong;
            }
            set
            {
                this._desoluong = value;
            }
        }

        public string pTEN
        {
            get
            {
                return this._sten;
            }
            set
            {
                this._sten = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "d_dmcachdung";
            }
        }
    }
}

